//
//  LCDeviceManagerVC.m
//  LongChain
//  设备管理
//  Created by FengZi on 2018/4/1.
//  Copyright © 2018年 FengZi. All rights reserved.
//

#import "LCDeviceManagerVC.h"
#import "LCDeviceManagerCell.h"

@interface LCDeviceManagerVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *deviceManagerCollection;

@property (nonatomic, strong) NSArray *deviceDatas;
@property (nonatomic, strong) UITextField *deviceName;
@property (nonatomic, strong) NSArray *bindDeviceArr;

@end

@implementation LCDeviceManagerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    LCLog(@"_devicesJson = %@",_devicesJson);

    if ([_devicesJson isKindOfClass:[NSString class]] && ![_devicesJson containsString:@"null"]) {
        NSData *nsData=[_devicesJson dataUsingEncoding:NSUTF8StringEncoding];
        
        NSArray *devicesArr = [NSJSONSerialization JSONObjectWithData:nsData options:kNilOptions error:nil];
        self.bindDeviceArr = devicesArr;

    }else{
        self.bindDeviceArr = @[];
    }

    _deviceDatas = @[
                     @[@"设备名称",@"设备状态",@"磁盘设置",@"设备信息"],
                     @[@"网络设置"],
                     @[@"重启郎链"],
                     @[@"解绑郎链"]
                     ];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateDeviceName:) name:UITextFieldTextDidChangeNotification object:nil];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    _deviceName = textField;
    LCLog(@"123 = %@",_deviceName.text);
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    
    LCLog(@"123 = %@",textField.text);
    return YES;
}
#pragma mark - - UICollectionViewDataSource UICollectionViewDelegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;

    LCDeviceManagerCell *deviceManagerCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"deviceManagerCell" forIndexPath:indexPath];
    if (_deviceDatas.count > section) {
        if (row == 0) {
            _deviceName = deviceManagerCell.deviceName;
        }
        NSArray *itemArr = _deviceDatas[section];
        if (itemArr.count > row) {
            [deviceManagerCell lc_updateDeviceManagerCellWithParameter:itemArr[row]];
        }
    }
    
    return deviceManagerCell;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _deviceDatas.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSArray *dataArr = _deviceDatas[section];
    return dataArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(SWidth, 50);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击");
    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;
    switch (section) {
        case 0:
        {
            switch (row) {
                case 2:  //  磁盘设置
                {
                    [self performSegueWithIdentifier:@"showDiskSetting" sender:self];
                }
                    break;
                case 3:  //  设备列表
                {
//                      showDeviceDetailInfo
                    [self performSegueWithIdentifier:@"showDeviceList" sender:self];
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case 1:  //  网络管理
        {
            [self performSegueWithIdentifier:@"showNetworkSetting" sender:self];
        }
            break;
        case 2:
        {
            switch (row) {
//                case 0:  //  安全弹出
//                {
//                    [self showTipWithType:UserActionTypeEjectHardDrive];
//
//                }
//                    break;
                case 0:  //  重启郎链
                {
                    [self showTipWithType:UserActionTypeRestartDevice];
                }
                    break;
//                case 1:  //  恢复默认设置
//                {
//                    [self showTipWithType:UserActionTypeRestoreDefaultSettings];
//                }
//                    break;
                    
                default:
                    break;
            }

        }
            break;
        case 3:  //  解绑郎链
        {
            [self showTipWithType:UserActionTypeSolutionBinding];

        }
            break;
            
        default:
            break;
    }
   
}

#pragma mark - -
- (void)showTipWithType:(UserActionType)type {

    NSString *title = @"";
    NSString *message = @"";
    NSString *sureButtonTitle = @"确定";
    switch (type) {
        case UserActionTypeEjectHardDrive:  //  弹出硬盘
        {
            title = @"温馨提示";
            message = @"确认安全弹出郎链,安全后您的郎链将不可再用";
        }
            break;
        case UserActionTypeRestoreDefaultSettings:  //  恢复默认设置
        {
            title = @"确认恢复默认设置?";
            message = @"该操作将:\n· 重启郎链(设备将暂时失去连接)\n· 您的硬盘数据仍会保留\n· 初始化郎链的下列设备:\n设备名称、网络设置、多媒体服务设置";
        }
            break;
        case UserActionTypeSolutionBinding:  //  解绑账号
        {
            title = @"确定解绑郎链";
            message = @"解绑后:\n·您将无法管理该设备\n·在设备为绑定期间您无法获得链克或水晶奖励\n已获得的奖励将保留在您的账号上\n是否继续解绑?";
            sureButtonTitle = @"确认解绑";
        }
            break;
        case UserActionTypeRestartDevice:  //  重启设备
        {
            title = @"确认重启郎链?";
            message = @"重启郎链后,不影响数据和账户的继续使用";
            sureButtonTitle = @"确认重启";
        }
            break;
        default:
            break;
    }
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:sureButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self untiedDeviceWithID:@""];
        
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:sureAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
}

//  解绑设备
- (void)untiedDeviceWithID:(NSString *)deveiceID {

    if (self.bindDeviceArr.count > 0) {
        NSDictionary *dic = [self.bindDeviceArr lastObject];
        LCLog(@"%@",dic);
        deveiceID = [NSString stringWithFormat:@"%@",dic[@"dev_id"]];
        NSDictionary *parameter = @{@"devid":deveiceID};
        [[LCNetwork shareInstance]lc_postNetworkWithRUL:@"/api/v0/device/unbind" parameter:parameter responseSuccess:^(id resultCode, id resultParameter) {
            LCLog(@"resultParameter = %@",resultParameter);

            if ([resultParameter isKindOfClass:[NSDictionary class]]) {
                NSString *code = [NSString stringWithFormat:@"%@",resultCode[@"code"]];
                if ([code isEqualToString:@"0"]) {
                    [self showHint:@"解绑成功"];
                }

            }
            
        } responseFail:^(id resultCode, NSError *error) {
            
        }];
    }else{
        [self showHint:@"请先绑定设备"];
    }
}

- (void)updateDeviceName:(NSNotification *)noti {
    
    LCDeviceManagerCell *deviceManagerCell = (LCDeviceManagerCell *)[self.deviceManagerCollection cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    _deviceName = deviceManagerCell.deviceName;
    
    LCLog(@"userInfo = %@",_deviceName.text);
}

- (IBAction)saveDeviceName:(id)sender {
    
    [self showHint:@"保存成功"];
}

- (IBAction)backAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
